package irri.soils.ssnm;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

public class ActivityK extends SSNM implements View.OnClickListener{
	private RadioButton radio1;
	private RadioButton radio2;
	private Button button1;
	private int option;
	private Bundle b;
	private Handler handler;
	private TextView question;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.k);
        setCustomTitle();
        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio1.setOnClickListener(this);
        radio2 = (RadioButton) findViewById(R.id.radio2);
        radio2.setOnClickListener(this);

        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button1.setEnabled(false);
        
        b = this.getIntent().getExtras();
        question = (TextView) findViewById(R.id.question);
        question.setText(get("basin0"));
        radio1.setText(get("yes"));
        radio2.setText(get("no"));
        if(b.containsKey("basin")){
	        switch(b.getInt("basin")){
	        case 1: radio1.performClick(); break;
	        case 2: radio2.performClick(); break;
	        }
	        button1.setEnabled(true);
        }
        
        handler=new Handler();
        final Runnable r = new Runnable()
        {
        	int i =0;
        	ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
            public void run() 
            {
            	imageView1.setImageResource(i%2==0 ? R.drawable.basin2 : R.drawable.basin);
                handler.postDelayed(this, 2000);
                i = (i+1)%2;
            }
        };
        handler.postDelayed(r, 2000);
    }

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.radio1: option = 1; button1.setEnabled(true); break;
		case R.id.radio2: option = 2; button1.setEnabled(true); break;
		case R.id.button1:
			Intent intent = 
				new Intent(ActivityK.this, ActivityM.class);
			b.remove("flood");
			b.putInt("basin", option);
			intent.putExtras(b);
			startActivity(intent);
			finish();
			break;
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(radio1.isChecked()) radio1.performClick();
		if(radio2.isChecked()) radio2.performClick();
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ActivityK.this, 
				ActivityJ.class);
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}
}